package fr.alesia.capital.servlet;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.alesia.capital.bean.Continent;
import fr.alesia.capital.bean.Partie;
import fr.alesia.capital.bean.Pays;
import fr.alesia.capital.factory.BeanFactory;
import fr.alesia.capital.factory.DaoFactory;

@SuppressWarnings("serial")
public class JeuServlet extends HttpServlet {
	
	private Continent continent;
	private int
		niveau,
		mode,
		questions
	;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		ServletContext context = this.getServletContext();
		HttpSession session = request.getSession();
		RequestDispatcher dispatcher = null;
		
		if(this.isInitialized(request)){
			
			this.readParameters(request);
			List<Pays> pays = this.pickUp(continent, niveau, questions);
			
			Partie partie = BeanFactory.createPartie(pays, continent, niveau, mode);
			
			session.setAttribute("partie", partie);
			
			dispatcher = context.getRequestDispatcher("/partie");
		}
		else{
	
			request.setAttribute("continents", Continent.values());
			dispatcher = context.getRequestDispatcher("/webapp/page/jeu.jsp");
		}
		
		dispatcher.forward(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		this.doPost(request, response);
	}
	
	private List<Pays> pickUp(Continent continent, int niveau, int questions){
		
		List<Pays> pays = DaoFactory.getPaysDao().getAll(continent, niveau);
		Collections.shuffle(pays);
		if(questions > 0 && pays.size() > questions){
			return pays.subList(0, questions);
		}
		else{
			return pays;
		}
	}
	
	private void readParameters(HttpServletRequest request){
		
		String
			sContinent = request.getParameter("continent"),
			sNiveau = request.getParameter("niveau"),
			sQuestions = request.getParameter("questions"),
			sMode = request.getParameter("mode")
		;
		
		if(sContinent == null || sContinent.length() == 0){
			continent = null;
		}
		else{
			continent = Continent.getContinent(sContinent);
		}
		
		if(sNiveau == null || sNiveau.length() == 0){
			niveau = 0;
		}
		else{
			try {
				niveau = Integer.parseInt(sNiveau);
			}
			catch (NumberFormatException e) {
				niveau = 0;
			}
		}
		
		if(sQuestions == null || sQuestions.length() == 0){
			questions = 10;
		}
		else{
			try {
				questions = Integer.parseInt(sQuestions);
			}
			catch (NumberFormatException e) {
				questions = 0;
			}
		}
		
		if(sMode == null || sMode.length() == 0){
			mode = Partie.PAYS_CAPITALE;
		}
		else{
			try {
				mode = Integer.parseInt(sMode);
			}
			catch (NumberFormatException e) {
				mode = 0;
			}
		}
	}
	
	private boolean isInitialized(HttpServletRequest request){
		
		return (request.getParameter("questions") != null);
	}
}
